<template>
  <el-dialog
    :title="{ add: '新增学科', edit: '编辑学科' }[mode]"
    :visible.sync="isShow"
    width="600px"
    :close="close"
  >
    <el-form ref="addForm" label-width="90px" :model="dataForm" :rules="rules">
      <el-form-item label="学科编号" prop="rid">
        <el-input v-model="dataForm.rid" />
      </el-form-item>
      <el-form-item label="学科名称" prop="name">
        <el-input v-model="dataForm.name" />
      </el-form-item>
      <el-form-item label="学科简称">
        <el-input v-model="dataForm.short_name" />
      </el-form-item>
      <el-form-item label="学科简介">
        <el-input v-model="dataForm.intro" />
      </el-form-item>
      <el-form-item label="学科备注">
        <el-input v-model="dataForm.remark" />
      </el-form-item>
    </el-form>
    <template>
      <div style="text-align: center">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      mode: '',
      isShow: false,
      dataForm: {
        rid: '', //	是	string	学科编号
        name: '', //	是	string	学科名称
        short_name: '', //	否	string	学科简称
        intro: '', // 否	string	学科简介
        remark: '' //	否	string	备注
      },
      rules: {
        rid: [{ required: true, message: '必填', trigger: 'change' }],
        name: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.addForm.validate(async(result) => {
        if (result) {
          if (this.mode === 'add') {
            await this.$api.subject.subjectAddPost(this.dataForm)
            this.$message.success('新增学科成功')
          } else if (this.mode === 'edit') {
            await this.$api.subject.subjectEditPost(this.dataForm)
            this.$message.success('编辑学科成功')
          }
          this.isShow = false
          this.$emit('refresh')
        }
      })
    }
  }
}
</script>

<style></style>
